﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ParaEditor.Desktop.Service.Entity;
using ParaEditor.Infrastructure;
using System.Data.OracleClient;

namespace ParaEditor.Desktop.Service.LogMgrService.Impl
{
    public class LogMgrServiceImpl
    {
        public void writeLog(string log_type, string log_level, string log_tmi, string log_content)
        {
            DateTime dt = DateTime.Now;
            string insertSql = "insert into TBL_CENTRE_LOG";
            insertSql += "(LOG_ID,LOG_TYPE,LOG_LEVEL,LOG_TIME,LOG_TMI,LOG_CONTENT) VALUES";
            insertSql += "(SEQ_LOG.NEXTVAL,'" + log_type + "','" + log_level + "','" + dt.ToString("yyyyMMddHHmmss") + "','" + log_tmi + "','" + log_content + "')";
            try
            {
                DataBase.SqlCommand(insertSql);
            }
            catch (Exception e)
            {

            }
        }

        public Logs GetLogsByDate(string startTime, string endTime)
        {
            OracleDataReader dataReader;
            Logs logs = new Logs();
            string sqlText = "SELECT LOG_ID,LOG_TYPE,LOG_LEVEL,to_char(to_date(LOG_TIME,'yyyyMMddHH24miss'),'yyyy-MM-dd HH24:mi:ss') LOG_DATE,LOG_TMI,LOG_CONTENT FROM TBL_CENTRE_LOG WHERE SUBSTR(LOG_TIME,1,8) BETWEEN '" + startTime + "' AND '" + endTime + "' order by LOG_ID DESC";
            try
            {
                dataReader = DataBase.SqlReader(sqlText);
                while (dataReader.Read())
                {
                    TBL_CENTRE_LOG _log = new TBL_CENTRE_LOG();
                    _log.LOG_ID = (int)dataReader.GetOracleNumber(0);
                    _log.LOG_TYPE = dataReader.GetOracleString(1).ToString();
                    _log.LOG_LEVEL = dataReader.GetOracleString(2).ToString();
                    _log.LOG_TIME = dataReader.GetOracleString(3).ToString();
                    _log.LOG_TMI = dataReader.GetOracleString(4).ToString();
                    _log.LOG_CONTENT = dataReader.GetOracleString(5).ToString();
                    logs.Add(_log);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            catch (Exception e)
            {
            }
            return logs;
        }
    }
}
